package org.stvd.entities.common;

import java.io.IOException;
import java.io.Serializable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

/**
 * 将接收的前端字符串类型转换成Long类型
 * @author houzx
 */
public class LongJsonDeserializer extends JsonDeserializer<Long> implements Serializable{

    private static final long serialVersionUID = -6833898439925666543L;
    
    private static final Logger logger = LoggerFactory.getLogger(LongJsonDeserializer.class);
    
    @Override
    public Long deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        String value = jsonParser.getText();
        try {
            return value == null ? null : Long.parseLong(value);
        } catch (NumberFormatException e) {
            logger.error("数据转换异常", e.getMessage());
            return null;
        }
    }
}
